﻿using Microsoft.Office.Interop.Word;
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;

namespace OfficeMiddlewareCore
{
    public class Core
    {

        private WsDto _wsDto;
        private Core(WsDto wsDto)
        {
            _wsDto = wsDto;
        }
        public static Core Create(WsDto wsDto)
        {
            var core = new Core(wsDto);
            return core;
        }

        /// <summary>
        /// 查看正文
        /// </summary>
        public void OpenWord()
        {
            var dto = _wsDto;

            WordHelper.Instance.OpenOnly(dto.LocalFileName);

        }
        public Tuple<Application, Document> Review(bool showRevisions = false)
        {
            var word = WordHelper.Instance.Open(_wsDto.LocalFileName, true);
            try
            {
                word.Item2.TrackRevisions = true;
                word.Item2.PrintRevisions = false;
                word.Item2.ShowRevisions = showRevisions;
                if (showRevisions)
                {
                    // word.Item1.ActiveWindow.View.MarkupMode=WdRevisionsMode

                }
                word.Item2.Save();
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.Message, ex);
            }
            return word;
        }

        /// <summary>
        /// 新建正文
        /// </summary>
        public Tuple<Application, Document> NewWord()
        {
            var dto = _wsDto;
            var word = WordHelper.Instance.Open(dto.LocalFileName, dto.IsReadOnly);
            word.Item1.UserName = dto.User;

            try
            {
                word.Item2.TrackRevisions = true;
                word.Item2.PrintRevisions = false;
                word.Item2.ShowRevisions = false;
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.Message, ex);
            }
            word.Item1.Activate();
            word.Item1.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
            WordHelper.Instance.Replace(word.Item2, dto.GetReplaceDic());
            word.Item1.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
            WordHelper.Instance.Replace(word.Item2, dto.GetReplaceDic());

            //AutoUpload(word.Item1, dto);
            return word;
        }


        public void OpenExcel(Action<bool> savedAction)
        {
            var dto = _wsDto;
            ExcelHelper.Instance.Open(dto.LocalFileName, savedAction);
        }

        /// <summary>
        /// 合并word
        /// </summary>
        public Tuple<Application, Document> MergeWord()
        {
            var dto = _wsDto;

            string zw;
            if (dto.FilePathExts.Count > 0)
            {
                zw = WordHelper.Instance.MergeDocument(dto.FilePathExts);
            }
            else
            {
                zw = dto.LocalFileName;
            }

            var app = WordHelper.Instance.Merge(zw, dto.GetRedFilePath(), dto.GetVerFilePath(), dto.CopyHeader);
            try
            {

                app.Item1.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
                WordHelper.Instance.Replace(app.Item2, dto.GetReplaceDic());
                app.Item1.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
                WordHelper.Instance.Replace(app.Item2, dto.GetReplaceDic());

                var doc = app.Item2;

                // doc.TrackRevisions = false;
                doc.PrintRevisions = false;
                doc.ShowRevisions = false;
                app.Item1.Visible = true;
                app.Item1.Activate();
            }
            catch (Exception ex)
            {
                app.Item1.Quit();
                throw ex;
            }


            //AutoUpload(app.Item1, dto);
            return app;
        }

        /// <summary>
        /// 清稿
        /// </summary>
        /// <param name="url"></param>
        public Tuple<Application, Document> Clean()
        {
            var dto = _wsDto;

            var app = WordHelper.Instance.Open(dto.LocalFileName, false);
            var doc = app.Item2;
            doc.TrackRevisions = false;
            doc.PrintRevisions = false;
            doc.ShowRevisions = false;
            var word = app.Item1;

            if (doc.ProtectionType != WdProtectionType.wdNoProtection)
            {
                doc.Unprotect();
            }
            doc.AcceptAllRevisions();
            //word.CommandBars["显示与隐藏痕迹"].Visible = false;
            if (doc.ProtectionType == WdProtectionType.wdNoProtection)
            {
                doc.Protect(WdProtectionType.wdAllowOnlyComments);
            }
            // AutoUpload(word, dto);
            word.Activate();
            return app;
        }

    }
}
